package com.llsydn.demo.sqliteBl;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.llsydn.hello.R;

import java.util.List;

/**
 * @author lilinshen
 * @title 黑名单的管理
 * @description 请填写相关描述
 * @date 2018/9/26 11:44
 */
public class SqliteBlActivity extends ListActivity {

    private ListView lv_main;
    private BlackNumberAdapter adapter;
    private BlackNumberDao dao;
    private List<BlackNumber> data;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_bl);
        //初始化数据
        lv_main = getListView(); //（使用系统自带的id：list）
        adapter = new BlackNumberAdapter();
        dao = new BlackNumberDao(this);
        data = dao.getAll();

        //设置adapter
        lv_main.setAdapter(adapter);

        //给ListView设置监听
        lv_main.setOnCreateContextMenuListener(this);
    }

    /**
     * 添加黑名单
     *
     * @param view
     */
    public void add(View view) {
        EditText editText = new EditText(this);
        editText.setHint("输入黑名单号码");
        new AlertDialog.Builder(this)
                .setTitle("添加黑名单号码")
                .setView(editText)
                .setPositiveButton("添加", (dialog, which) -> {
                    //1.保存数据到数据库表中
                    String number = editText.getText().toString();
                    BlackNumber blackNumber = new BlackNumber(-1, number);
                    dao.add(blackNumber);
                    //2.保存数据到list中（保存在第一位中）
                    data.add(0, blackNumber);
                    //3.通知更新列表
                    adapter.notifyDataSetChanged();
                })
                .setNegativeButton("取消", null)
                .show();
    }

    /**
     * 自定义的BaseAdapter
     */
    class BlackNumberAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = View.inflate(SqliteBlActivity.this, android.R.layout.simple_list_item_1, null);
            }
            BlackNumber blackNumber = data.get(position);
            TextView textView = convertView.findViewById(android.R.id.text1);
            textView.setText(blackNumber.getNumber());
            return convertView;
        }
    }

    /******************长按键(ContextMenu)***************************/
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        //得到长按的position
        AdapterView.AdapterContextMenuInfo adapterContextMenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
        position = adapterContextMenuInfo.position;
        menu.add(0, 1, 0, "更新");
        menu.add(0, 2, 0, "删除");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //得到BlackNumber
        BlackNumber blackNumber = data.get(position);
        switch (item.getItemId()) {
            case 1:
                showUpdateDialog(blackNumber);
                Toast.makeText(SqliteBlActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                //1).删除数据表对应的数据
                dao.deleteById(blackNumber.getId());
                //2).删除List对应的数据
                data.remove(position);
                //3).通知更新列表
                adapter.notifyDataSetChanged();
                Toast.makeText(SqliteBlActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onContextItemSelected(item);
    }

    /**
     * 显示更新的对话框
     *
     * @param blackNumber
     */
    private void showUpdateDialog(final BlackNumber blackNumber) {
        EditText editText = new EditText(this);
        editText.setHint(blackNumber.getNumber());
        new AlertDialog.Builder(this)
                .setTitle("更新黑名单号码")
                .setView(editText)
                .setPositiveButton("更新", (dialog, which) -> {
                    //1.更新数据到list中
                    String number = editText.getText().toString();
                    blackNumber.setNumber(number);
                    //2.保存数据到数据库表中
                    dao.update(blackNumber);
                    //3.通知更新列表
                    adapter.notifyDataSetChanged();
                })
                .setNegativeButton("取消", null)
                .show();
    }
}
